/*

Version:
Author: LH Software Group
Copyright
*/

//#include "LH3001.h"
#include <stdio.h>
//#include "uart_print.h"
#include "uart_frame.h"
#include "process_cmd.h"

static uint32_t data_cnt;

static ADC_DATA_NOFIFO_t dat;
//volatile uint32_t data_ready_flag;
static UART_CMD_t uart_proc;

void delay(uint32_t t)
{
    for(uint32_t i=0;i<t;i++);
}

///*check below registers for configuration*/
//REG_DUMP_t reg[] =
//{
//	{ADDR_LH3001_SYSCLK,0,"ADDR_LH3001_SYSCLK"},
//  {ADDR_LH3001_PGAGAIN,0,"ADDR_LH3001_PGAGAIN"},
//  {ADDR_LH3001_PGACTRL,0,"ADDR_LH3001_PGACTRL"},
//  {ADDR_LH3001_PGACTRL1,0,"ADDR_LH3001_PGACTRL1"},
//  {ADDR_LH3001_CONFIG1,0,"ADDR_LH3001_CONFIG1"},
//  {ADDR_LH3001_ADCCTRL,0,"ADDR_LH3001_ADCCTRL"},
//  {ADDR_LH3001_ADCCHCON,0,"ADDR_LH3001_ADCCHCON"},
//  {ADDR_LH3001_BUFCON,0,"ADDR_LH3001_BUFCON"},
//  {ADDR_LH3001_MODCLK,0,"ADDR_LH3001_MODCLK"},
//  {ADDR_LH3001_CLKCON,0,"ADDR_LH3001_CLKCON"},
//  {ADDR_LH3001_SPICTRL,0,"ADDR_LH3001_SPICTRL"},
//	{ADDR_LH3001_LOCON1,0,"ADDR_LH3001_LOCON1"},
//  {ADDR_LH3001_LOCON2,0,"ADDR_LH3001_LOCON2"},
//  {ADDR_LH3001_LOCON3,0,"ADDR_LH3001_LOCON3"},
//  {ADDR_LH3001_LOFFSTAT,0,"ADDR_LH3001_LOFFSTAT"},
//  {ADDR_LH3001_RLDCON,0,"ADDR_LH3001_RLDCON"},
//  {0x4e,0,"ADDR_LH3001_TEST1"},
//};



int lh001_drv_init(void)
{
	uint8_t val;
    uart_proc.flag = LH_FRAME_PROC_FLAG_INVALID;

//	data_ready_flag = 0;

    LH3001_Spi_Init();
	lh3001_hw_reset();
	
	lh3001_read_regs(ADDR_LH3001_BUFCON,&val,1);
    val &= ~(MSK_LH3001_BUFCON_EN2P5|MSK_LH3001_BUFCON_ENREF);
    lh3001_write_regs(ADDR_LH3001_BUFCON,&val,1); 

    uart_fifo_init();
//	data_ready_flag = 0;

    
    
//    while(1)
//    {

        uart_frame_process(&uart_proc);
//        if(uart_proc.flag == LH_FRAME_PROC_FLAG_VALID)
//        {
//            process_cmd(&uart_proc);
//        }
//			
//		if(data_ready_flag>0)
//		{
//			data_ready_flag = 0;
//            process_adc_data(&uart_proc,dat);
//		}		 
//    }
    return 0;
}


//void EXTI4_15_IRQHandler()
//{
//  if(exti_interrupt_flag_get(EXTI_10) != RESET)
//  {
//    /* Clear the EXTI line 0 pending bit */
//    exti_interrupt_flag_clear(EXTI_10);
//    
//    //read ADC data
//    lh3001_read_data_nofifo(&dat);
//    data_ready_flag++;
//  }
//    
//}

//void USART1_IRQHandler()
//{
//    /* USART in Receiver mode */
//	if(usart_flag_get(USART1,USART_FLAG_RBNE)!= RESET)
//  {
//      uart_fifo_write( usart_data_receive(USART1) );
//  }
//}
